package com.hxh.generater.springBoot.simple.gen;

import cn.hutool.core.util.StrUtil;
import com.hxh.entity.Project;
import com.hxh.entity.ProjectDatasource;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.common.GenSysMenu;
import com.hxh.generater.springBoot.genBase.GenTableMenu;
import com.hxh.util.MysqlUtils;

import java.util.Date;

public class GenSimpleTableMenu implements GenTableMenu {

    private static final String TABLE_MENU_NAME = "sys_menu";
    private static GenSysMenu getSysMenuDirInput(ClassInfo classInfo) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu sysMenuInput = new GenSysMenu();
        sysMenuInput.setParentId(0L);
        sysMenuInput.setTreePath("0");
        sysMenuInput.setName(classInfo.getDesc() + "管理");
        sysMenuInput.setType("2");
        sysMenuInput.setPath("/" + className);
        sysMenuInput.setComponent("Layout");
        sysMenuInput.setPerm(className);
        sysMenuInput.setAlwaysShow("1");
        sysMenuInput.setKeepAlive("1");
        sysMenuInput.setVisible("1");
        sysMenuInput.setSort(0);
        sysMenuInput.setIcon("product-attr");
        sysMenuInput.setCreateTime(new Date());
        sysMenuInput.setUpdateTime(new Date());
        return sysMenuInput;
    }

    private static GenSysMenu getSysMenuDeleteInput(int id, GenSysMenu menu, ClassInfo classInfo) {
        GenSysMenu deleteMenu = new GenSysMenu();
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        deleteMenu.setParentId((long) id);
        deleteMenu.setTreePath(menu.getTreePath()+","+ id);
        deleteMenu.setName("删除"+ classInfo.getDesc());
        deleteMenu.setType("4");
        deleteMenu.setPath("/" + className+"/delete");
        deleteMenu.setPerm( className+":delete");
        deleteMenu.setAlwaysShow("1");
        deleteMenu.setKeepAlive("1");
        deleteMenu.setVisible("1");
        deleteMenu.setSort(0);
        deleteMenu.setCreateTime(new Date());
        deleteMenu.setUpdateTime(new Date());
        return deleteMenu;
    }

    private static GenSysMenu getSysMenuUpdateInput(int id, GenSysMenu menu, ClassInfo classInfo) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu updateMenu = new GenSysMenu();
        updateMenu.setParentId((long) id);
        updateMenu.setTreePath(menu.getTreePath()+","+ id);
        updateMenu.setName("修改"+ classInfo.getDesc());
        updateMenu.setType("4");
        updateMenu.setPath("/" + className+"/update");
        updateMenu.setPerm( className+":update");
        updateMenu.setAlwaysShow("1");
        updateMenu.setKeepAlive("1");
        updateMenu.setVisible("1");
        updateMenu.setSort(0);
        updateMenu.setCreateTime(new Date());
        updateMenu.setUpdateTime(new Date());
        return updateMenu;
    }

    private static GenSysMenu getSysMenuAddInput(int id, GenSysMenu menu, ClassInfo classInfo) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu addMenu = new GenSysMenu();
        addMenu.setParentId((long) id);
        addMenu.setTreePath(menu.getTreePath()+","+ id);
        addMenu.setName("添加"+ classInfo.getDesc());
        addMenu.setType("4");
        addMenu.setPath("/" + className+"/add");
        addMenu.setPerm( className+":add");
        addMenu.setAlwaysShow("1");
        addMenu.setKeepAlive("1");
        addMenu.setVisible("1");
        addMenu.setSort(0);
        addMenu.setCreateTime(new Date());
        addMenu.setUpdateTime(new Date());
        return addMenu;
    }
    private static GenSysMenu getSysMenuBatchImportInput(int id, GenSysMenu menu, ClassInfo classInfo) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu addMenu = new GenSysMenu();
        addMenu.setParentId((long) id);
        addMenu.setTreePath(menu.getTreePath()+","+ id);
        addMenu.setName("批量添加"+ classInfo.getDesc());
        addMenu.setType("4");
        addMenu.setPath("/" + className+"/batchImport");
        addMenu.setPerm( className+":batchImport");
        addMenu.setAlwaysShow("1");
        addMenu.setKeepAlive("1");
        addMenu.setVisible("1");
        addMenu.setSort(0);
        addMenu.setCreateTime(new Date());
        addMenu.setUpdateTime(new Date());
        return addMenu;
    }

    private static GenSysMenu getSysMenuBatchExportInput(int id, GenSysMenu menu, ClassInfo classInfo) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu addMenu = new GenSysMenu();
        addMenu.setParentId((long) id);
        addMenu.setTreePath(menu.getTreePath()+","+ id);
        addMenu.setName("批量导出"+ classInfo.getDesc());
        addMenu.setType("4");
        addMenu.setPath("/" + className+"/batchExport");
        addMenu.setPerm( className+":batchExport");
        addMenu.setAlwaysShow("1");
        addMenu.setKeepAlive("1");
        addMenu.setVisible("1");
        addMenu.setSort(0);
        addMenu.setCreateTime(new Date());
        addMenu.setUpdateTime(new Date());
        return addMenu;
    }

    private static GenSysMenu getSysMenuDirInput(int id, ClassInfo classInfo) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu menu = new GenSysMenu();
        menu.setParentId((long) id);
        menu.setTreePath("0,"+ id);
        menu.setName(classInfo.getDesc() + "管理");
        menu.setType("1");
        menu.setPath("/" + className+"/listPage");
        menu.setComponent(className+"/index");
        menu.setPerm(className+":listPage");
        menu.setAlwaysShow("1");
        menu.setKeepAlive("1");
        menu.setVisible("1");
        menu.setSort(0);
        menu.setIcon("product-attr");
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        return menu;
    }

    @Override
    public void initTableMenu(Project project, ClassInfo classInfo) throws Exception {
        ProjectDatasource projectDatasource = project.getProjectDatasource();

        String path = "/" + StrUtil.toCamelCase(classInfo.getTableName());
        Object o = MysqlUtils.queryOneAndReturnFirstResult(projectDatasource, "select count(*) from sys_menu where path = '" + path + "';");
        if (o != null) {
            if (Integer.parseInt(o.toString()) > 0) {
                return;
            }
        }

        GenSysMenu sysMenuInput = getSysMenuDirInput(classInfo);
        String sql = MysqlUtils.genInsertSql(sysMenuInput, TABLE_MENU_NAME);

        int id = MysqlUtils.executeInsertSqlWithId(projectDatasource,sql);

        //添加菜单
        GenSysMenu menu = getSysMenuDirInput(id, classInfo);
        sql = MysqlUtils.genInsertSql(menu, TABLE_MENU_NAME);
        id = MysqlUtils.executeInsertSqlWithId(projectDatasource,sql);

        //添加增加按钮
        GenSysMenu addMenu = getSysMenuAddInput(id, menu, classInfo);
        sql = MysqlUtils.genInsertSql(addMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource,sql);

        //修改增加按钮
        GenSysMenu updateMenu = getSysMenuUpdateInput(id, menu, classInfo);
        sql = MysqlUtils.genInsertSql(updateMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource,sql);

        //删除增加按钮
        GenSysMenu deleteMenu = getSysMenuDeleteInput(id, menu, classInfo);
        sql = MysqlUtils.genInsertSql(deleteMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource,sql);

        GenSysMenu importMenu = getSysMenuBatchImportInput(id, menu, classInfo);
        sql = MysqlUtils.genInsertSql(importMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource,sql);

        GenSysMenu exportMenu = getSysMenuBatchExportInput(id, menu, classInfo);
        sql = MysqlUtils.genInsertSql(exportMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource,sql);
    }
}
